package com.hualian.coupon.service.impl;

import com.hualian.coupon.mapper.ManagerMapper;
import com.hualian.coupon.pojo.Manager;
import com.hualian.coupon.service.ManagerService;
import com.hualian.coupon.util.MD5Util;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author Lxd
 * @Description TODO
 * @date 2020/10/22 16:03
 * @Version 1.0
 */
@Service("managerService")
public class ManagerServiceImpl implements ManagerService {

    @Resource
    ManagerMapper managerMapper;
    @Override
    public List<Manager> find(Manager manager) {
        Example example = new Example(Manager.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orLike("name", "%"+manager.getName()+"%");
        criteria.orLike("phone", "%"+manager.getPhone()+"%");
        return managerMapper.selectByExample(example);
    }

    @Override
    public int add(String name,String phone) {
        Manager manager = new Manager();
        manager.setName(name);
        manager.setPhone(phone);
        manager.setRegDate(new Date());
        String psw = phone.substring(phone.length() - 6);
        manager.setPassword(MD5Util.encode(psw));
        return managerMapper.insertSelective(manager);
    }

    @Override
    public Manager findByPhone(String phone) {
        Manager manager = new Manager();
        manager.setPhone(phone);
        return managerMapper.selectOne(manager);
    }

    @Override
    public int update(Manager manager) {
        Example example = new Example(Manager.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone",manager.getPhone());
        return managerMapper.updateByExampleSelective(manager,example);
    }

    @Override
    public int delete(String phone) {
        Example example = new Example(Manager.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone", phone);
        return managerMapper.deleteByExample(example);
    }
}
